import {useState, useEffect} from 'react'
import db from '../db'

const UPPER = 3 //连续认出次数超过这个设置的用户才会被继续保留。

export default (classname) => {
  const [students, setStudents] = useState([])
  const [total, setTotal] = useState([])
  const [finished, setFinished] = useState(false)
  useEffect(() => {
    db.students.where('classname').equals(classname)
      .toArray()
      .then(arr => {
        setTotal(arr)
        const filtered = arr.filter(({recognized}) => recognized <= UPPER)
        setStudents(filtered)
        if(arr.length > 0 && filtered.length === 0) {
          setFinished(true)
        }
      })
      .catch(err => console.log(err))
  }, [classname])
  return {students, finished, total}
}
